package com.stormrage.edu.dao.exam.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.exam.ExmExamDao;
import com.stormrage.entity.exam.ExmExam;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: ExmExamDaoImpl.java
 * @Package com.stormrage.edu.dao.exam.impl
 * @Description: 考试试题问题 Dao
 * @author www.stormragetech.com
 * @date 2016年4月24日
 * @version V1.0
 */
@Repository
public class ExmExamDaoImpl extends MysBaseDao implements ExmExamDao {

	private static final Logger logger = LoggerFactory.getLogger(ExmExamDaoImpl.class);

	@Override
	public Integer create(final ExmExam arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 Create 】- 开始运行 ");

		final String query = "INSERT INTO exm_exam (question, type, relate_type, relate_id) VALUES"
				+ " (:question, :type, :relateType, :relateId)";
		logger.debug(" 数据库执行语句为 " + query);

		Integer index = create(query, arg0);

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 Create 】- 完成运行 ");
		return index;

	}

	@Override
	public void update(final ExmExam arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 update 】- 开始运行 ");

		final String query = "UPDATE exm_exam SET question = :question, type = :type WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 update 】- 完成运行 ");

	}

	@Override
	public void delete(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 update 】- 开始运行 ");

		final String query = "DELETE FROM exm_exam WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, map);

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 update 】- 完成运行 ");

	}

	@Override
	public Integer queryCountByRelate(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 queryCountByRelateIdLimit 】- 开始运行 ");

		final String query = "SELECT COUNT(*) FROM exm_exam WHERE relate_type = :relateType AND relate_id = :relateId"
				+ map.get("query");
		logger.debug(" 数据库执行语句为 " + query);

		Integer count = queryCount(query, map);

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 queryCountByRelateIdLimit 】- 完成运行 ");
		return count;

	}

	@Override
	public Integer queryCountByRelateType(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 queryCountByRelateType 】- 开始运行 ");

		final String query = "SELECT COUNT(*) FROM exm_exam WHERE type = :type AND relate_type = :relateType"
				+ " AND relate_id = :relateId";
		logger.debug(" 数据库执行语句为 " + query);

		Integer count = queryCount(query, map);

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 queryCountByRelateType 】- 完成运行 ");
		return count;

	}

	@Override
	public ExmExam queryById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 queryById 】- 开始运行 ");

		final String query = "SELECT * FROM exm_exam WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		ExmExam entity = queryObject(query, ExmExam.class, map);

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 queryById 】- 完成运行 ");
		return entity;

	}

	@Override
	public List<ExmExam> queryListByRelateId(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 queryListByRelateId 】- 开始运行 ");

		final String query = "SELECT * FROM exm_exam WHERE relate_id = :relateId";
		logger.debug(" 数据库执行语句为 " + query);

		List<ExmExam> array = queryList(query, ExmExam.class, map);

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 queryListByRelateId 】- 完成运行 ");
		return array;

	}

	@Override
	public List<ExmExam> queryListByRelateLimit(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 queryListByRelateIdLimit 】- 开始运行 ");

		final String query = "SELECT * FROM exm_exam WHERE relate_type = :relateType AND relate_id = :relateId"
				+ map.get("query") + " LIMIT :startPage, :size";
		logger.debug(" 数据库执行语句为 " + query);

		List<ExmExam> array = queryList(query, ExmExam.class, map);

		logger.debug(" 【 Dao 】-【 ExmExamDao 】-【 queryListByRelateIdLimit 】- 完成运行 ");
		return array;

	}

}
